<?php
namespace app\wechat\controller;
use think\Controller;
use think\Db;
use think\Request;
class Token extends Controller
{
    protected $table_user_sms_info= 'user_sms_info';         /* 验证码信息表 */
    protected $table_user_bind_info= 'user_bind_info';       /* 绑定手机号 */
    protected $table_accounts_info= 'accounts_info';         /* 账户表 */
    protected $table_user_lock_info= 'user_lock_info';        
    protected $table_user_gift_record= 'user_gift_record';   /* 用户兑换表 */
    protected $table_user_score_log= 'user_score_log';       /*用户金币记录表*/
    protected $table_user_prop_log= 'user_prop_log';         /*用户道具记录表*/
    protected $table_user_mail_info= 'user_mail_info';       /*用户道具记录表*/
    protected $table_user_props_info= 'user_props_info';     /*用户道具记录表*/
    protected $user_control_list = 'user_control_list';      /*黑白名单*/
    protected $table_user_game_info = 'user_game_info';      /*渠道配置表*/
    protected $table_DB_qptreasuredb = 'qptreasuredb';       /*充值记录库*/
    protected $user_recharge_info = 'recharge_info';         /* 充值记录表 */
    protected function Db_connect_user_sms_info(){
        $user_sms_info= Db::connect('db_qpplatformdb')->table($this->table_user_sms_info);
        return $user_sms_info;
    }
    protected function Db_connect_user_control_list(){
        $user_control_list= Db::connect('db_config2')->table($this->user_control_list);
        return $user_control_list;
    }
    protected function Db_connect_user_gift_record(){
        $user_gift_record= Db::connect('db_qpplatformdb')->table($this->table_user_gift_record);
        return $user_gift_record;
    }
    protected function Db_connect_user_mail_info(){
        $user_mail_info= Db::connect('db_qpplatformdb')->table($this->table_user_mail_info);
        return $user_mail_info;
    }
    protected function Db_connect_accounts_info(){
        $accounts_info= Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_lock_info()
    {
        $user_game_info = Db::connect('db_qpplatformdb')->table($this->table_user_lock_info);
        return $user_game_info;
    }
    protected function Db_connect_user_props_info(){
        $accounts_info= Db::connect('db_config2')->table($this->table_user_props_info);
        return $accounts_info;
    }
    protected function Db_connect_user_bind_info(){
        $user_bind_info= Db::connect('db_config2')->table($this->table_user_bind_info);
        return $user_bind_info;
    }
    protected function Db_connect_table_accounts_info(){
        $accounts_info= Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_score_log(){
        $user_score_log= Db::connect('db_config3')->table($this->table_user_score_log);
        return $user_score_log;
    }
    protected function Db_connect_user_prop_log(){
        $user_prop_log= Db::connect('db_config3')->table($this->table_user_prop_log);
        return $user_prop_log;
    }
    protected function Db_connect_user_game_info(){
        $user_game_info= Db::connect('db_config2')->table($this->table_user_game_info);
        return $user_game_info;
    }
    protected function Db_connect_recharge_info(){
        $recharge_info= Db::connect($this->table_DB_qptreasuredb)->table($this->user_recharge_info);
        return $recharge_info;
    }


    private $current_server = null;
    const SERVER_TEST = 'http://192.168.1.181:10000/';
    const SERVER_A = 'http://8.219.138.113:10000/';
    const SERVER_B = 'http://8.219.79.180:10000/';
    public function index(Request $request)
    {
        $param = $request->param();
        $token =$param['token'];
        if($token=='92sdjflk4654344s56d4fsdfjlk'){
            $Platform_online_count_A = 0; 
            $url ='http://8.219.138.113:10000/?id=13';
            $res = curl_get($url);
            if($res){
                $arr = json_decode($res, true);
                if($arr){
                    $Platform_online_count_A =count($arr);
                }
            }

            $Platform_online_count_B = 0; 
            $url_B ='http://8.219.79.180:10000/?id=13';
            $res_B = curl_get($url_B);
            if($res_B){
                $arr_B = json_decode($res_B, true);
                if($arr_B){
                    $Platform_online_count_B =count($arr_B);
                }
            }
            $this->assign('Platform_online_count_A', $Platform_online_count_A);
            $this->assign('Platform_online_count_B', $Platform_online_count_B);

            $Platform_online_count_C = 0; 
            $url_C ='http://121.196.33.246:10000/?id=13';
            $res_C = curl_get($url_C);
            if($res_C){
                $arr_C = json_decode($res_C, true);
                if($arr_C){
                    $Platform_online_count_C =count($arr_C);
                }
            }
            $this->assign('Platform_online_count_C', $Platform_online_count_C);

            $room_list = $this->get_room_list(); 

            $today_am =date("Y-m-d",time());
            $today_recharge_amout= db('tradelog','db_config2')->where('paydate','>',$today_am)->where('status','=',1)->sum('amount');
            /*昨日充值金额*/
            $yesterday_recharge_amout= db('tradelog','db_config2')->where('paydate','between',[date("Y-m-d",strtotime('-1 day')),date("Y-m-d 23:59:59",strtotime('-1 day'))])->where('status','=',1)->sum('amount');

            // 今日活跃
            $today_num = db('accounts_info','db_config2')->where('LastLoginDate','>=',$today_am)->count();
 
            // 昨日活跃 
            $yesterday_num = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-1 day')))->where('channel','=',888)->value('dayactivity');
            // 今日新增
            $today_reg = db('accounts_info','db_config2')->where('RegisterDate','>=',$today_am)->count();
 
            // 昨日新增 
            $yesterday_reg = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-1 day')))->where('channel','=',888)->value('dru');
            // 次留
            $remain_1day = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-2 day')))->where('channel','=',888)->value('second_day');
            
            // 3留
            $remain_3day = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-3 day')))->where('channel','=',888)->value('third_day');
            // 7留
            $remain_7day = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-7 day')))->where('channel','=',888)->value('seventh_day');
            // 15留
            $remain_15day = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-15 day')))->where('channel','=',888)->value('fifth_day');
            // 30留
            $remain_30day = db('stat_remain','db_config2')->where('stat_time','=',date('Y-m-d',strtotime('-30 day')))->where('channel','=',888)->value('thirtieth_day');
            // 话费兑换
            $phone_bill_total = $this->Db_connect_user_gift_record()->field("sum(

                case
                        WHEN Describle = '爱奇艺月卡' 
                        OR Describle = '腾讯视频月卡' 
                        OR Describle = '优酷视频月卡' 
                        THEN 19 
                        WHEN Describle = '爱奇艺季卡' 
                        OR Describle = '腾讯视频季卡' 
                        OR Describle = '腾讯视频季卡' 
                        THEN 58 
                        WHEN Describle = '爱奇艺年卡' 
                        OR Describle = '腾讯视频年卡' 
                        OR Describle = '优酷视频年卡' THEN 190
                        else
                        cast(Describle as UNSIGNED INTEGER)
                    end

            ) as total")
            ->whereTime('InsertTime','today')
            ->find();

            $this->assign('today_recharge_amout', $today_recharge_amout);
            $this->assign('today_num', $today_num);
            $this->assign('yesterday_num',$yesterday_num);
            $this->assign('today_reg', $today_reg);
            $this->assign('yesterday_reg',$yesterday_reg);
            $this->assign('remain_1day', 100*$remain_1day.'%');
            $this->assign('remain_3day', 100*$remain_3day.'%');
            $this->assign('remain_7day', 100*$remain_7day.'%');
            $this->assign('remain_15day', 100*$remain_15day.'%');
            $this->assign('remain_30day', 100*$remain_30day.'%');
            $this->assign('room_list', $room_list);
            $this->assign('yesterday_recharge_amout', $yesterday_recharge_amout);
            $this->assign('phone_bill_total', $phone_bill_total['total']);
            return  $this->fetch();  
        }else{
            echo "I am sorry!!!!";
        }

    }

    // public function save()
    // {
    //     if($this->request->isPost()){
    //         $data = $this->request->post();
    //         if($data['UserID']==''){
    //             return $this->error('用户ID不能为空');
    //         }
    //         $data_user_info['UserID']=$data['UserID'];
    //         $data_user_info['Cannon']=$data['Cannon'];
    //         $this->Db_connect_user_game_info()->update($data_user_info);
    //         $user =$this->Db_connect_accounts_info()->where("UserID",$data['UserID'])->find();
    //         if(empty($user)){
    //             $result['code'] = 0;
    //             $result['msg'] = '账户不存在';
    //             return json($result);exit;
    //         }
    //         $data_Arr['UserID']=$data['UserID'];
    //         $data_Arr['Lottery']=$data['Lottery'];
    //         $data_Arr['LotteryStock']=$data['LotteryStock'];
    //         $data_Arr['VipLevel']=$data['VipLevel']>10?10:$data['VipLevel'];
    //         $data_Arr['Level']=$data['Level'];
    //         $data_Arr['Score']=$data['Score'];
    //         $data_Arr['Diamond']=$data['Diamond'];
    //         $data_Arr['NickName']=$data['NickName'];
    //         $this->Db_connect_accounts_info()->update($data_Arr);
    //         /*青铜弹头*/
    //         $Bronze_arr=$this->Db_connect_user_props_info()->where('PropID',10016)->where('UserID',$data['UserID'])->find();
    //         if(empty($Bronze_arr)){
    //             $data_Bronze_array['UserID']=$data['UserID'];
    //             $data_Bronze_array['PropID']=10016;
    //             $data_Bronze_array['Counts']=$data['Bronze_Warhead'];
    //             $this->Db_connect_user_props_info()->insert($data_Bronze_array);
    //         }else{
    //             $this->Db_connect_user_props_info()->where('PropID',10016)->where('UserID',$data['UserID'])->setField('Counts',$data['Bronze_Warhead']);
    //         }
    //         /*白银弹头*/
    //         $Silver_arr=$this->Db_connect_user_props_info()->where('PropID',10017)->where('UserID',$data['UserID'])->find();
    //         if(empty($Silver_arr)){
    //             $data_Silver_array['UserID']=$data['UserID'];
    //             $data_Silver_array['PropID']=10017;
    //             $data_Silver_array['Counts']=$data['Silver_warhead'];
    //             $this->Db_connect_user_props_info()->insert($data_Silver_array);
    //         }else{
    //             $this->Db_connect_user_props_info()->where('PropID',10017)->where('UserID',$data['UserID'])->setField('Counts',$data['Silver_warhead']);
    //         }
    //         /*黄金弹头*/
    //         $Gold_arr=$this->Db_connect_user_props_info()->where('PropID',10018)->where('UserID',$data['UserID'])->find();
    //         if(empty($Gold_arr)){
    //             $data_Gold_array['UserID']=$data['UserID'];
    //             $data_Gold_array['PropID']=10018;
    //             $data_Gold_array['Counts']=$data['Gold_Warhead'];
    //             $this->Db_connect_user_props_info()->insert($data_Gold_array);
    //         }else{
    //             $this->Db_connect_user_props_info()->where('PropID',10018)->where('UserID',$data['UserID'])->setField('Counts',$data['Gold_Warhead']);
    //         }
    //         /*核弹*/
    //         $Nuclear_bomb_arr=$this->Db_connect_user_props_info()->where('PropID',10041)->where('UserID',$data['UserID'])->find();
    //         if(empty($Nuclear_bomb_arr)){
    //             $Nuclear_bomb_array['UserID']=$data['UserID'];
    //             $Nuclear_bomb_array['PropID']=10041;
    //             $Nuclear_bomb_array['Counts']=$data['Nuclear_bomb'];
    //             $this->Db_connect_user_props_info()->insert($Nuclear_bomb_array);
    //         }else{
    //             $this->Db_connect_user_props_info()->where('PropID',10041)->where('UserID',$data['UserID'])->setField('Counts',$data['Nuclear_bomb']);
    //         }
    //         /* 黑名单 */
    //         $black_arr['Type']=$data['black_type'];
    //         $res=$this->Db_connect_user_control_list()->where('UserID',$data['UserID'])->find();
    //         if($res){
    //             $this->Db_connect_user_control_list()->where('UserID',$data['UserID'])->update($black_arr);
    //         }else{
    //             $data_arr['Type']=$data['black_type'];
    //             $data_arr['UserID']=$data['UserID'];
    //             $data_arr['RecordTime']=date('Y-m-d H:i:s',time());
    //             $this->Db_connect_user_control_list()->insert($data_arr);
    //         }

    //         /*时光石*/
    //         $Time_bomb_arr=$this->Db_connect_user_props_info()->where('PropID',10005)->where('UserID',$data['UserID'])->find();
    //         if(empty($Time_bomb_arr)){
    //             $data_Time_bomb_array['UserID']=$data['UserID'];
    //             $data_Time_bomb_array['PropID']=10005;
    //             $data_Time_bomb_array['Counts']=$data['Time_bomb'];
    //             $this->Db_connect_user_props_info()->insert($data_Time_bomb_array);
    //         }else{
    //             $this->Db_connect_user_props_info()->where('PropID',10005)->where('UserID',$data['UserID'])->setField('Counts',$data['Time_bomb']);
    //         }

    //         $result = $this->Db_connect_user_lock_info()->where('UserID', $data['UserID'])->find();
    //         $server_id = $this->Db_connect_accounts_info()->where('UserID', $data['UserID'])->value('LoginServer');
           
            
    //         if (empty($result)) {
    //                 $data_ar['RecoverStock'] = $data['RecoverStock'];
    //                 $res = $this->Db_connect_accounts_info()->where('UserID', $data['UserID'])->update($data_ar);
    //         } else {
    //             if (env('IS_DEVELOP') == 1) {
    //                 $current_server = self::SERVER_TEST;
    //             } else {
    //                 if ($server_id == '1') {
    //                     $current_server = self::SERVER_A;
    //                 } else if ($server_id == '2') {
    //                     $current_server = self::SERVER_B;
    //                 }
    //             }
    //             $data_Arr = [
    //                 'Type'=>3,
    //                 'UserID' => (int) $data['UserID'],
    //                 'Score' => (int) $data['RecoverStock']
    //             ];
    //             $url_param = json_encode($data_Arr);
    //             /*测试*/
    //             $ceshi_url = $current_server . '?id=14&data=';
    //             $param = $ceshi_url . $url_param;
    //             $res = curl_get($param);
    //             trace($res,'error');
    //         }

    //         $dump_str['player'] = $data['UserID'];
    //         $content_str['Score'] = $data['Score'];
    //         $content_str['RecoverStock'] = $data['RecoverStock'];
    //         $content_str['black_type'] = $data['black_type'];
    //         $content_str['VipLevel'] = $data['VipLevel'];

    //         $content_str['10016']=$data['Bronze_Warhead'];
    //         $content_str['10017']=$data['Silver_warhead'];
    //         $content_str['10018']=$data['Gold_Warhead'];

    //         $dump_str['content'] = '修改:'.json_encode($content_str);

    //         $dump_str['admin'] = 'hezong';
    //         $dump_str['add_time'] = date('Y-m-d H:i:s');
    //         db('admin_player_log','db_operatelog')->insert($dump_str);

    //         $result['code'] = 1;
    //         $result['msg'] = '应用成功';
    //         return json($result);exit;
    //     }
    // }
    function search(){
        if($this->request->isPost()){
            $data = $this->request->post();
            
            if(!empty($data['UserID'])){
                $user =$this->Db_connect_accounts_info()->where("UserID",$data['UserID'])->find();
            }elseif (!empty($data['NickName'])){
                $user =$this->Db_connect_accounts_info()->where("NickName",$data['NickName'])->order('RegisterDate desc')->find();
            }else{
                return $this->error('用户ID和昵称不能同时为空');
            }

            $desc = db('spreader_config','db_qpplatformdb',false)->where('SpreaderID','=',$user['SpreaderID'])->value('Desc');

            $user['SpreaderID'] = empty($desc) ?$user['SpreaderID']: $desc; 

            $data['UserID'] = $user['UserID'];

            /*注册城市*/
            $key = '5191f666df421265f5b673281a97454c';
            $type = 'json';
            $url = 'https://restapi.amap.com/v3/ip?ip='.$user['RegisterIP'].'&output='.$type.'&key='.$key;
            $info = file_get_contents($url);
            $jsonAddress =json_decode($info,true);
            /*登录城市*/
            $login_url = 'https://restapi.amap.com/v3/ip?ip='.$user['LastLoginIP'].'&output='.$type.'&key='.$key;
            $login_info = file_get_contents($login_url);
            $login_jsonAddress =json_decode($login_info ,true);
            if(empty($jsonAddress['province'])){
                $jsonAddress['province']='暂无城市';
            }
            if(empty($jsonAddress['city'])){
                $jsonAddress['city']='';
            }
            if(empty($login_jsonAddress['province'])){
                $login_jsonAddress['province']='暂无城市';
            }
            if(empty($login_jsonAddress['city'])){
                $login_jsonAddress['city']='';
            }
            $user['RegisterIP_city']=$jsonAddress['province'].$jsonAddress['city'];
            $user['LastLogin_city']=$login_jsonAddress['province'].$login_jsonAddress['city'];
            $user_list_black =$this->Db_connect_user_control_list()->field('Type')->where('UserID',$data['UserID'])->find();
            if(empty($user_list_black)){
                $user['black_type']= 0;
            }else{
                $user['black_type']= $user_list_black['Type'];
            }
            $user_Cannon=$this->Db_connect_user_game_info()->field('Cannon')->where('UserID',$data['UserID'])->find();
            if(empty($user_Cannon)){
                $user['Cannon']= 0;
            }else{
                $user['Cannon']= $user_Cannon['Cannon'];
            }
            $Bronze_Warhead=$this->Db_connect_user_props_info()->where('PropID',10016)->where('UserID',$data['UserID'])->value('Counts');
            if(empty($Bronze_Warhead)){
                $Bronze_Warhead=0;
            }
            $Silver_warhead=$this->Db_connect_user_props_info()->where('PropID',10017)->where('UserID',$data['UserID'])->value('Counts');
            if(empty($Silver_warhead)){
                $Silver_warhead=0;
            }
            $Gold_Warhead=$this->Db_connect_user_props_info()->where('PropID',10018)->where('UserID',$data['UserID'])->value('Counts');
            if(empty($Gold_Warhead)){
                $Gold_Warhead=0;
            }

            $Time_bomb=$this->Db_connect_user_props_info()->where('PropID',10005)->where('UserID',$data['UserID'])->value('Counts');
            if(empty($Time_bomb)){
                $Time_bomb=0;
            }

            $Nuclear_bomb =$this->Db_connect_user_props_info()->where('PropID',10041)->where('UserID',$data['UserID'])->value('Counts');
            if(empty($Nuclear_bomb)){
                $Nuclear_bomb=0;
            }
            $result['code'] = 1;
            $result['data'] = $user;
            $result['Bronze_Warhead'] = $Bronze_Warhead;
            $result['Silver_warhead'] = $Silver_warhead;
            $result['Gold_Warhead'] = $Gold_Warhead;
            $result['Time_bomb'] = $Time_bomb;
            $result['Nuclear_bomb'] = $Nuclear_bomb;
            return json($result);exit;
        }
    }

    public function get_room_list(){
        $roomsA = [
            '1'=>'风暴港湾1',
            '2'=>'蟹将传说2',
            '3'=>'龙女夺宝3',
            '4'=>'龙神传说4',
            '5'=>'风暴港湾5',
            '6'=>'蟹将传说6',
            '7'=>'龙女夺宝7',
            '8'=>'龙神传说8',
            '9'=>'风暴港湾9',
            '10'=>'蟹将传说10',
            '11'=>'龙女夺宝11',
            '12'=>'龙神传说12',
            '61'=>'玉石场',
            '90'=>'一夜暴富',
            '91'=>'深海龙宫',
            '92'=>'藏宝海湾',
            '20'=>'竞技场1',
            '21'=>'竞技场2',
            '93'=>'宝石迷城',
            '38'=>'打地鼠38',
            '39'=>'打地鼠39',
            '40'=>'怒海争锋',
        ];

        $data = [];

        $url_a ='http://8.219.138.113:10000/?id=13';
        $arr_a = json_decode(curl_get($url_a), true);
        if($arr_a){
            $data1 = array_count_values(array_column($arr_a,'ServerID'));
            ksort($data1);
            foreach($data1 as $k=>$v){
                $data[$k] = $v;
            }
        }

        $url_b ='http://8.219.79.180:10000/?id=13';
        $arr_b = json_decode(curl_get($url_b), true);
        if($arr_b){
            $data2 = array_count_values(array_column($arr_b,'ServerID'));
            ksort($data2);
            foreach($data2 as $k=>$v){
                $data[$k] = $v;
            }
        }

        $url_c ='http://121.196.33.246:10000/?id=13';
        $arr_c = json_decode(curl_get($url_c), true);
        if($arr_c){
            $data3 = array_count_values(array_column($arr_c,'ServerID'));
            ksort($data3);
            foreach($data3 as $k=>$v){
                $data[$k] = $v;
            }
        }
       
        $res = [];
        foreach($roomsA as $k=>$v){
            $tmp = [];
            $tmp['room'] = $v;
            // 服务器id在原来基础上加100
            $tmp['A'] = isset($data[$k])?$data[$k]:0;
            $tmp['B'] = isset($data[100+$k])?$data[100+$k]:0;
            $tmp['C'] = isset($data[200+$k])?$data[200+$k]:0;
            array_push($res,$tmp);
        }

        // arsort($res);
        
        return $res;
    }

}
